package sq.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sq.dao.Address;
import sq.dao.Food;
import sq.dao.SchoolAddress;
import sq.service.AddressService;
import sq.vo.BaseRespVO;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: sunquan
 * @Date: 2018/3/28
 * @Time: 22:06
 */
@RestController
public class AddressController {
@Autowired
private AddressService addressService;
    /**
     * 查询学校地址
     */
    @PostMapping(value = "/schoolAddress")
    public BaseRespVO findSchoolAddress() {
        BaseRespVO baseRespVO = new BaseRespVO();
        ArrayList<SchoolAddress> schoolAddresses=(ArrayList<SchoolAddress>) addressService.findSchoolAddress();
        baseRespVO.setCode("0");
        baseRespVO.setData(schoolAddresses);
        baseRespVO.setMsg("查询成功");
        return baseRespVO;
    }

    /**
     * 更新地址
     */
    @RequestMapping(value = "/address/update", method = RequestMethod.POST)
    @ResponseBody
    public BaseRespVO<Food> addressUpdate(Integer userId,String phone,String username,Integer schoolAddress,String addressDetail) {
        BaseRespVO<Food> baseRespVO = new BaseRespVO<Food>();
        addressService.updateAddress(userId,phone,username,schoolAddress,addressDetail);
        baseRespVO.setCode("0");
        baseRespVO.setMsg("更新成功");
        return baseRespVO;
    }
    /**
     * 查询地址
     */
    @PostMapping(value = "/address")
    public BaseRespVO findAddress(HttpServletRequest request) {
        BaseRespVO baseRespVO = new BaseRespVO();
        Address address=null;
        address=addressService.findAddress(request);
        if(address!=null){
            baseRespVO.setCode("0");
            baseRespVO.setData(address);
            baseRespVO.setMsg("查询成功");
        }
        else{
            baseRespVO.setCode("1");
            baseRespVO.setMsg("查询失败");
        }
        return baseRespVO;
    }
}
